//
//  GroupManagerViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/7.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "GroupManagerViewController.h"
#import "GroupManagerTableViewCell.h"

#define kCurrentUserToken [[NSUserDefaults standardUserDefaults]objectForKey:@"token"]

@interface GroupManagerViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UITableView *groupManagerTableView;

@property (nonatomic, strong) UIView *blackBackGround; // 灰色背景
@property (nonatomic, strong) NSString *isAddOrEdit; // 是添加还是编辑
@property (nonatomic, strong) NSIndexPath *selectIndexPath; // 记录被选中的行
@property (nonatomic, strong) NSString *textChange; // 记录text里面的内容

@property (nonatomic, assign) __block BOOL canClickAddGroup; // 是否可以点击添加分组
@property (nonatomic, assign) __block BOOL canClickDeleteGroup; // 是否可以点击删除分组
@property (nonatomic, assign) __block BOOL canClickEditGroup; // 是否可以点击编辑分组

@property (nonatomic, assign) BOOL shouldRefresh; // 是否需要刷新我的好友界面

@end

@implementation GroupManagerViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 添加数据源
    [self addData];
    // 布局
    [self addMySubview];
    
    self.canClickAddGroup = YES;
    self.canClickDeleteGroup = YES;
    self.canClickEditGroup = YES;
    // 默认不需要刷新我的好友界面
    self.shouldRefresh = NO;
    
    // 删除我的好友这个分组，因为我的好友的名字是不能变的
    for (int i = 0; i < self.groupArray.count; i++) {
        NSString *groupName = self.groupArray[i];
        if ([groupName isEqualToString:@"我的好友"]) {
            [self.groupArray removeObjectAtIndex:i];
            // 删除对应数组里面的内容
            [self.groupNameDuiYingDic removeObjectForKey:groupName];
        }
    }
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"分组管理";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
//  UIBarButtonItem *rightBarItem =  [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(finish:)];
//    rightBarItem.tintColor = [UIColor whiteColor];
//    self.navigationItem.rightBarButtonItem = rightBarItem;
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    if (self.shouldRefresh == YES) {
        self.groupManagerVCBlock(@"1");
        [[NetStatusHandle shareHandle] setShouldFreshOnLianXiRen:@"YES"];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 完成
- (void)finish:(UIBarButtonItem *)barItem
{
    NSLog(@"完成");
}

#pragma mark - 添加数据源
- (void)addData
{
//    self.groupArray = [NSMutableArray arrayWithObjects:@"我的小学同学", @"我的初中同学", @"我的高中同学", nil];
//    self.groupArray = [NSMutableArray array];
//    
//    // 存放分组的ID
//    self.groupNameDuiYingDic = [NSMutableDictionary dictionary];
}

#pragma mark - 布局
- (void)addMySubview
{
    self.groupManagerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 * kAdjustParamater)];
    self.groupManagerTableView.backgroundColor = [UIColor whiteColor];
    self.groupManagerTableView.dataSource = self;
    self.groupManagerTableView.delegate = self;
  
    [self.view addSubview:_groupManagerTableView];
    
    // 构建表头
    UIView *whiteHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 85 * kAdjustParamater)];
    whiteHeaderView.backgroundColor = [UIColor whiteColor];
    whiteHeaderView.userInteractionEnabled = YES;
    self.groupManagerTableView.tableHeaderView = whiteHeaderView;
    
    // 添加图片
    UIImageView *addImageV = [[UIImageView alloc] initWithFrame:CGRectMake(33 * kAdjustParamater, 15 * kAdjustParamater, 55 * kAdjustParamater, 55 * kAdjustParamater)];
    addImageV.userInteractionEnabled = YES;
    addImageV.image = [UIImage imageNamed:@"huisetianjiakuang-fenzuguanli"];
  //  addImageV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [whiteHeaderView addSubview:addImageV];
    UITapGestureRecognizer *tapGesInImageV = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addGroup:)];
    [addImageV addGestureRecognizer:tapGesInImageV];
    
    // 添加分组
    UILabel *addGroupLabel = [[UILabel alloc] initWithFrame:CGRectMake(addImageV.right + 20 * kAdjustParamater, 32 * kAdjustParamater, 100 * kAdjustParamater, 20 * kAdjustParamater)];
    addGroupLabel.text = @"添加分组";
    addGroupLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    addGroupLabel.textAlignment = NSTextAlignmentLeft;
    addGroupLabel.font = [UIFont systemFontOfSize:k20Big];
    [whiteHeaderView addSubview:addGroupLabel];
    
    UILabel *grayLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 84 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
    grayLine.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    [whiteHeaderView addSubview:grayLine];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.groupArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50 * kAdjustParamater;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier =  @"groupManager";
    GroupManagerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[GroupManagerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.groupArray.count > indexPath.row) {
        cell.groupNameLabel.text = self.groupArray[indexPath.row];
    }
    [cell.deleteButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.deleteButton.tag = 300;
    [cell.editButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.editButton.tag = 301;
    return cell;
}

#pragma mark - cell按钮点击
- (void)clickButton:(UIButton *)button
{
    UIImageView *imageV = (UIImageView *)[button superview];
    UIView *contentV = (UIView *)[imageV superview];
    GroupManagerTableViewCell *cell = (GroupManagerTableViewCell *)[contentV superview];
    NSIndexPath *indexPath = [self.groupManagerTableView indexPathForCell:cell];
  //  NSLog(@"section = %ld, row = %ld", indexPath.section, indexPath.row);
    switch (button.tag) {
        case 300:
        {
            NSLog(@"删除, section = %ld, row = %ld", indexPath.section, indexPath.row);
            
            [self alertOfDeleteGroup:indexPath];
            break;
        }
            case 301:
        {
            NSLog(@"编辑, section = %ld, row = %ld",  indexPath.section, indexPath.row);
            self.isAddOrEdit = @"EDIT";
            self.selectIndexPath = indexPath;
            [self addAlert];
            break;
        }
        default:
            break;
    }
}

// 删除群组的提醒框
- (void)alertOfDeleteGroup:(NSIndexPath*) indexPath
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:@"您确定要删除本群组么" preferredStyle:UIAlertControllerStyleAlert];
    // 修改标题颜色
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"提醒"];
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertC setValue:title forKey:@"attributedTitle"];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    // 修改取消按钮的颜色
    // [cancelAction setValue:[UIColor cyanColor] forKey:@"titleTextColor"];
    [alertC addAction:cancelAction];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (self.canClickDeleteGroup == YES) {
            self.canClickDeleteGroup = NO;
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
            // 删除分组的url
            NSString *deleteGroupUrl = [NSString stringWithFormat:@"%@?token=%@", kZGWDeleteGroupUrl, kCurrentUserToken];
            // 取出对应的分组的id
            NSString *groupName = self.groupArray[indexPath.row];
            NSString *groupID = [self.groupNameDuiYingDic valueForKey:groupName];
            NSDictionary *paramaterDic = @{@"friend_group_id":groupID};
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            [manager DELETE:deleteGroupUrl parameters:paramaterDic success:^(NSURLSessionDataTask *task, id responseObject) {
                NSDictionary *statusDic = [responseObject valueForKey:@"status"];
                NSString *msgString = [statusDic valueForKey:@"msg"];
                if ([msgString isEqualToString:@"delete success"]) {
                    // 说明删除成功
                    // 首先删除对应分组字典里面的数据
                    [self.groupNameDuiYingDic removeObjectForKey:groupName];
                    [self.groupArray removeObjectAtIndex:indexPath.row];
                    [self.groupManagerTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
                    self.shouldRefresh = YES;
                }
                self.canClickDeleteGroup = YES;
                [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                self.canClickDeleteGroup = YES;
                [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
                [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"提示" Content:@"删除失败，请重试" ToView:self];
            }];
        }
        
        
    }];
    [alertC addAction:deleteAction];
    [self presentViewController:alertC animated:YES completion:nil];

}

//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *identifier = @"myCell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }
//    cell.textLabel.text = self.groupArray[indexPath.row];
//    return cell;
//}
//
//
//
//// http://blog.jobbole.com/67272/
//// 多项编辑
//- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    // 点击之前都把选中行设为-1
//    self.selectIndexPath = nil;
//    
//    
//    // 编辑
//    UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        NSLog(@"编辑");
//        self.isAddOrEdit = @"EDIT";
//        self.selectIndexPath = indexPath;
//        [self addAlert];
//    }];
//    edit.backgroundColor = kCyanColor;
//    
//    // 删除
//    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        NSLog(@"删除");
//        // 删除数组
//        [self.groupArray removeObjectAtIndex:indexPath.row];
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//        
//    }];
//    delete.backgroundColor = [UIColor redColor];
//    
//    
//    return @[edit, delete];
//}

#pragma mark - 添加分组
- (void)addGroup:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"添加分组");
    self.isAddOrEdit = @"ADD";
    [self addAlert];
}

#pragma mark - 添加弹出视图
- (void)addAlert
{
    self.blackBackGround = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.blackBackGround.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.view addSubview:_blackBackGround];
    
    UITapGestureRecognizer *canceltapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAlert:)];
    [self.blackBackGround addGestureRecognizer:canceltapGes];
    // 说明是添加
    UIView *whiteBackGroundView = [[UIView alloc] initWithFrame:CGRectMake(55 / 2 * kAdjustParamater, 100 * kAdjustParamater, 320 * kAdjustParamater, 185 * kAdjustParamater)];
    whiteBackGroundView.userInteractionEnabled = YES;
    whiteBackGroundView.backgroundColor = [UIColor whiteColor];
    whiteBackGroundView.layer.cornerRadius = 5 * kAdjustParamater;
    [self.blackBackGround addSubview:whiteBackGroundView];
    
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((320 - 100) / 2 * kAdjustParamater, 15 * kAdjustParamater, 100 * kAdjustParamater, 15 * kAdjustParamater)];
    //  titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:k16Big];
    titleLabel.textColor = [UIColor blackColor];
    [whiteBackGroundView addSubview:titleLabel];
    
    UILabel *shuRuLable = [[UILabel alloc] initWithFrame:CGRectMake((320 - 150) / 2 * kAdjustParamater, titleLabel.bottom + 15 * kAdjustParamater, 150 * kAdjustParamater, 15 * kAdjustParamater)];
    //  shuRuLable.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    
    shuRuLable.textAlignment = NSTextAlignmentCenter;
    shuRuLable.font = [UIFont systemFontOfSize:k16Big];
    shuRuLable.textColor = [UIColor blackColor];
    [whiteBackGroundView addSubview:shuRuLable];
    
    // 输入框背景
    UILabel *shuRuBackGround = [[UILabel alloc] initWithFrame:CGRectMake((320 - 275) / 2 * kAdjustParamater, shuRuLable.bottom + 15 * kAdjustParamater, 275 * kAdjustParamater, 45 * kAdjustParamater)];
    shuRuBackGround.userInteractionEnabled = YES;
    shuRuBackGround.layer.cornerRadius = 5 * kAdjustParamater;
    shuRuBackGround.layer.borderColor = [[[UIColor blackColor] colorWithAlphaComponent:0.1] CGColor];
    shuRuBackGround.layer.borderWidth = 2 * kAdjustParamater;
    [whiteBackGroundView addSubview:shuRuBackGround];
    
    // 输入框
    UITextField *shuRuTF = [[UITextField alloc] initWithFrame:CGRectMake(10 * kAdjustParamater, 12 * kAdjustParamater, 255 * kAdjustParamater, 20 * kAdjustParamater)];
    shuRuTF.placeholder = @"请输入分组名";
    shuRuTF.clearsOnBeginEditing = YES;
    shuRuTF.font = [UIFont systemFontOfSize:k16Big];
    shuRuTF.delegate = self;
    // 添加监控
    [shuRuTF addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventAllEditingEvents];
    [shuRuBackGround addSubview:shuRuTF];
    
    
    
    
    // 横着的灰色条
    UILabel *hengGrayLine = [[UILabel alloc] initWithFrame:CGRectMake(0, shuRuBackGround.bottom + 22 * kAdjustParamater, whiteBackGroundView.width, 1 * kAdjustParamater)];
    hengGrayLine.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [whiteBackGroundView addSubview:hengGrayLine];
    
    // 竖着的灰色条
    UILabel *shuZheGrayLine = [[UILabel alloc] initWithFrame:CGRectMake((320 - 1) / 2 * kAdjustParamater, hengGrayLine.bottom, 1 * kAdjustParamater, 45 * kAdjustParamater)];
    shuZheGrayLine.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [whiteBackGroundView addSubview:shuZheGrayLine];
    
    // 取消按钮
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(10 * kAdjustParamater, hengGrayLine.bottom, 140 * kAdjustParamater, 43 * kAdjustParamater);
   //    cancelButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:kCyanColor forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:k16Big];
    [cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [whiteBackGroundView addSubview:cancelButton];
    
    // 确定按钮
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
    doneButton.frame = CGRectMake((160 + 10) * kAdjustParamater, cancelButton.top, 140 * kAdjustParamater, 43 * kAdjustParamater);
    // doneButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [doneButton setTitle:@"确定" forState:UIControlStateNormal];
    [doneButton setTitleColor:kCyanColor forState:UIControlStateNormal];
    doneButton.titleLabel.font = [UIFont systemFontOfSize:k16Big];
    
    [doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
    [whiteBackGroundView addSubview:doneButton];
    
    // 构建添加分组
    if ([self.isAddOrEdit isEqualToString:@"ADD"]) {
        titleLabel.text = @"添加分组";
        shuRuLable.text = @"请输入新的分组名称";
    } else if ([self.isAddOrEdit isEqualToString:@"EDIT"]) {
        titleLabel.text = @"编辑分组";
        shuRuLable.text = @"请输入新的分组名称";
    }
    

    
}

#pragma mark - 取消
- (void)cancel:(UIButton *)button
{
    NSLog(@"取消");
    [self.blackBackGround removeFromSuperview];
}

#pragma mark - 确定
- (void)done:(UIButton *)button
{
    if (self.textChange.length == 0 || [self.textChange containsString:@" "]) {
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"提示" Content:@"内容不能为空并且不能包含空格" ToView:self];
    } else {
        if ([self.isAddOrEdit isEqualToString:@"ADD"]) {
            
            // 防止多次点击添加
            if (self.canClickAddGroup == YES) {
                self.canClickAddGroup = NO;
                [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
                // 添加分组的url
                NSString *addGroupUrl = [NSString stringWithFormat:@"%@?token=%@", kZGWAddGroupUrl, kCurrentUserToken];
                NSDictionary *paramaterDic = @{@"group_name":self.textChange};
                AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
                [manager POST:addGroupUrl parameters:paramaterDic success:^(NSURLSessionDataTask *task, id responseObject) {
                    NSDictionary *statusDic = [responseObject valueForKey:@"status"];
                    NSString *msgString = [statusDic valueForKey:@"msg"];
                    if ([msgString isEqualToString:@"group created"]) {
                        NSDictionary *groupDic = [responseObject valueForKey:@"group"];
                        NSString *groupName = [groupDic valueForKey:@"name"];
                        NSString *groupID = [NSString stringWithFormat:@"%@", [groupDic valueForKey:@"id"]];
                        NSLog(@"分组添加成功, 分组的名字：%@，分组的ID：%@", groupName, groupID);
                        // 需要添加一行
                        [self.groupArray insertObject:groupName atIndex:0];
                        [self.groupNameDuiYingDic setValue:groupID forKey:groupName];
                        // 添加到第一行
                        [self.groupManagerTableView reloadData];
                        self.shouldRefresh = YES;
                    } else if ([msgString isEqualToString:@"group is existed"]) {
                        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"提示" Content:@"该分组已经存在，请重新添加" ToView:self];
                    }
                    self.canClickAddGroup = YES;
                    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
                } failure:^(NSURLSessionDataTask *task, NSError *error) {
                    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"提示" Content:@"添加分组失败，请重试" ToView:self];
                    self.canClickAddGroup = YES;
                    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
                }];
                
                
            }
            
            
        } else if ([self.isAddOrEdit isEqualToString:@"EDIT"]) {
            // 需要编辑被选中的
            if (self.selectIndexPath != nil) {
                
                if (self.canClickEditGroup == YES) {
                    self.canClickEditGroup = NO;
                    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
                    // 编辑分组的url
                    NSString *editGroupUrl = [NSString stringWithFormat:@"%@?token=%@", kZGWEditGroupUrl, kCurrentUserToken];
                    // 取出对应的小组名字和小组I
                    
                    NSString *groupName = self.groupArray[self.selectIndexPath.row];
                    NSString *groupID = [self.groupNameDuiYingDic valueForKey:groupName];
                    
                    NSDictionary *paramaterDic = @{@"friend_group_id":groupID, @"group_name":self.textChange};
                    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
                    [manager POST:editGroupUrl parameters:paramaterDic success:^(NSURLSessionDataTask *task, id responseObject) {
                        NSDictionary *statusDic = [responseObject valueForKey:@"status"];
                        NSString *msgString = [statusDic valueForKey:@"msg"];
                        if ([msgString isEqualToString:@"update success"]) {
                            NSDictionary *friend_groupDic = [responseObject valueForKey:@"friend_group"];
                            NSLog(@"编辑之前的分组名字:%@", self.groupArray);
                            NSLog(@"编辑之前的分组字典:%@", self.groupNameDuiYingDic);
                            // 更改过后的名字
                            NSString *afterGengGaiName = [friend_groupDic valueForKey:@"name"];
                            // 首先删除更改之前对应名字字典里的数据
                            NSString *beforeGengGaiName = self.groupArray[self.selectIndexPath.row];
                            [self.groupNameDuiYingDic removeObjectForKey:beforeGengGaiName];
                            // 替换
                            [self.groupArray replaceObjectAtIndex:self.selectIndexPath.row withObject:afterGengGaiName];
                            // 增加对应字典里的数据
                            [self.groupNameDuiYingDic setValue:groupID forKey:afterGengGaiName];
                            NSLog(@"编辑之后的分组名字:%@", self.groupArray);
                            NSLog(@"编辑之后的分组字典:%@", self.groupNameDuiYingDic);
                            [self.groupManagerTableView reloadData];
                            self.shouldRefresh = YES;
                        }
                        self.canClickEditGroup = YES;
                        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
                    } failure:^(NSURLSessionDataTask *task, NSError *error) {
                        self.canClickEditGroup = YES;
                        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
                        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"提示" Content:@"分组编辑失败，请重试" ToView:self];
                        
                    }];
                }
                
            }
        }
        [self.blackBackGround removeFromSuperview];
        NSLog(@"确定");
    }
    
}

#pragma mark - 取消弹出框
- (void)cancelAlert:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"取消弹出框");
    [tapGes.view removeFromSuperview];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - TF监控
- (void)changeText:(UITextField *)textField
{
    self.textChange = textField.text;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
